package com.google.code.p.narcissus.coreIntegrationTests.utils;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;

public class IntegrationTestConfiguration {

	private static IntegrationTestConfiguration instance;

	private static final Logger LOGGER = Logger.getLogger(IntegrationTestConfiguration.class);

	private SeleniumServer server;

	public static IntegrationTestConfiguration getInstance() throws Exception {
		if (instance == null) {
			LOGGER.getLoggerRepository().resetConfiguration();
			DOMConfigurator.configure(IntegrationTestConfiguration.class.getResource("/log4j-narcissus-core-integration-tests.xml"));

			instance = new IntegrationTestConfiguration();
			RemoteControlConfiguration config = new RemoteControlConfiguration();
			config.setPort(4444);

			instance.server = new SeleniumServer(false, config);

		}

		return instance;
	}

	private IntegrationTestConfiguration() {
	}

	public void startServer() {
		try {
			server.start();
		} catch (Exception e) {
			throw new RuntimeException("Error starting the Selenium Server!  See stacktrace for more info...", e);
		}
	}

	public void stopServer() {
		server.stop();
	}
}
